package com.example.mvp.mvppromote.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mvp.mvppromote.R;
import com.example.mvp.mvppromote.model.WeiXinItemBean;
import com.example.mvp.mvppromote.ui.activity.WebViewActivity;
import com.example.mvp.mvppromote.utils.GlideCircleTransform;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wty on 2016/6/23 0023.
 */
public class WeiXinAdapter extends RecyclerView.Adapter<WeiXinAdapter.ViewHolder> {
    private ArrayList<WeiXinItemBean> weiXinItemBeans;
    private Context context;

    public WeiXinAdapter(ArrayList<WeiXinItemBean> weiXinItemBeans, Context context) {
        this.weiXinItemBeans = weiXinItemBeans;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.grid_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(holder.itemView.getContext()).load(weiXinItemBeans.get(position).getPicUrl()).transform(new GlideCircleTransform(context, 6)).into(viewHolder.imageIv);
        viewHolder.descriptionTv.setText(weiXinItemBeans.get(position).getTitle());
        viewHolder.descriptionTime.setText(weiXinItemBeans.get(position).getHottime());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url", weiXinItemBeans.get(position).getUrl());
                intent.putExtra("title", weiXinItemBeans.get(position).getTitle());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return weiXinItemBeans.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageIv)
        ImageView imageIv;
        @BindView(R.id.descriptionTv)
        TextView descriptionTv;
        @BindView(R.id.descriptionTime)
        TextView descriptionTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
